/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package libase.web.jsf.bean;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Named;
import javenue.csv.Csv;
import libase.entity.Book;
import libase.web.DateFormat;

/**
 *
 * @author aspr0
 */
@Named
@Stateless
public class BookBean extends AbstractBean
{
    private Book b = new Book();
    private String exportCsvMsg;
    private String exportCsvLink;
    private String exportCsvLinkText;
    @EJB
    private PreferenceBean preferenceBean;
    @EJB
    private ExemplarBean exemplarBean;

    public Book getB()
    {
        if (fetchId())
        {
            return b = em.find(Book.class, id);
        }

        return b;
    }

    public void setB(Book b)
    {
        this.b = b;
    }

    public List<Book> getBooks()
    {
        return em.createNamedQuery("Book.findAll").getResultList();
    }

    public void create()
    {
        em.persist(b);
        b = new Book();
    }

    public void edit()
    {
        em.merge(b);
        b = new Book();
    }

    public void remove()
    {
        getB();
        em.remove(b);
        b = new Book();
    }

    /**
     * Exports available books report to csv file
     */
    public void exportCsv()
    {
        exportCsvMsg = "Unable to export. ";
        exportCsvLink = ".";

        String outDir;

        try
        {
            outDir = preferenceBean.getOutputDir();
        }
        catch (PreferenceException e)
        {
            exportCsvMsg += e.getMessage();
            return;
        }
        catch (IOException e)
        {
            exportCsvMsg += e.getMessage();
            return;
        }

        List<Book> books = getBooks();
        Iterator<Book> i = books.iterator();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
        String dateString = dateFormat.format(new Date());
        String fileName = String.format("available.%s.csv", dateString);
        String filePath = outDir + fileName;
        exportCsvLink = String.format("/csv/%s", fileName);
        exportCsvLinkText = fileName;

        Csv.Writer writerCsv = new Csv.Writer(filePath).delimiter(';');
        writerCsv
                    .value("Id")
                    .value("Name")
                    .value("Publisher")
                    .value("Publish date")
                    .value("Author")
                    .value("Available")
                    .newLine();

        while (i.hasNext())
        {
            Book book = i.next();

            writerCsv
                    .value(book.getId().toString())
                    .value(book.getName())
                    .value(book.getPub().toString())
                    .value(DateFormat.asY(book.getPubDt()))
                    .value(book.getAuth().toString())
                    .value(exemplarBean.countAvail(book.getId()).toString())
                    .newLine();
        }

        writerCsv.close();

        exportCsvMsg = "Export successful.";
    }

    public String getExportCsvMsg()
    {
        return exportCsvMsg;
    }

    public String getExportCsvLink()
    {
        return exportCsvLink;
    }

    public String getExportCsvLinkText()
    {
        return exportCsvLinkText;
    }
}
